package com.bussiness.controller;

import com.bussiness.config.annotions.MyRoleAuth;
import com.common.response.ResponseBean;
import com.common.response.impl.BaseResponseImpl;
import com.common.util.mypage.Pages;
import com.quartz.entity.AppQuartz;
import com.quartz.util.QuartzUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-02 11:29
 **/
@RestController
@Api(tags={"quartz测试"})
@MyRoleAuth
@RequestMapping("/quartz")
public class QuartzController extends BaseResponseImpl {

    private static Logger log = LoggerFactory.getLogger(QuartzController.class);

    @Autowired
    private QuartzUtil quartzUtil;

    @ApiOperation(value="获取任务列表")
    @RequestMapping(value = "/queryJob", method = RequestMethod.GET)
    public ResponseBean queryJob( Integer pageNumber, Integer pageSize){
        Pages pages = new Pages(pageNumber, pageSize, quartzUtil.jobList());
        return successData(pages);
    }

    @ApiOperation(value="添加任务")
    @RequestMapping(value = "/addJob", method = RequestMethod.PUT)
    public ResponseBean addJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.addJob(appQuartz);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    @ApiOperation(value="修改任务")
    @RequestMapping(value = "/modifyJob", method = RequestMethod.PUT)
    public ResponseBean modifyJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.modifyJob(appQuartz);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    @ApiOperation(value="删除任务")
    @RequestMapping(value = "/deleteJob", method = RequestMethod.DELETE)
    public ResponseBean deleteJob(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobDelete(jobName, jobGroup);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    @ApiOperation(value="暂停任务")
    @RequestMapping(value = "/jobPause", method = RequestMethod.PUT)
    public ResponseBean jobPause(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobPause(jobName, jobGroup);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    @ApiOperation(value="恢复任务")
    @RequestMapping(value = "/jobResume", method = RequestMethod.PUT)
    public ResponseBean jobResume(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobResume(jobName, jobGroup);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }



}
